<template>
  <el-dialog
    :title="!dataForm.supplierId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="供应商编码" prop="supplierCode">
      <el-input v-model="dataForm.supplierCode" placeholder="供应商编码"></el-input>
    </el-form-item>
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input v-model="dataForm.supplierName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="供应商类型：1.自营，2.平台" prop="supplierType">
      <el-input v-model="dataForm.supplierType" placeholder="供应商类型：1.自营，2.平台"></el-input>
    </el-form-item>
    <el-form-item label="供应商联系人" prop="linkMan">
      <el-input v-model="dataForm.linkMan" placeholder="供应商联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="供应商开户银行名称" prop="bankName">
      <el-input v-model="dataForm.bankName" placeholder="供应商开户银行名称"></el-input>
    </el-form-item>
    <el-form-item label="银行账号" prop="bankAccount">
      <el-input v-model="dataForm.bankAccount" placeholder="银行账号"></el-input>
    </el-form-item>
    <el-form-item label="供应商地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="供应商地址"></el-input>
    </el-form-item>
    <el-form-item label="状态：0禁止，1启用" prop="supplierStatus">
      <el-input v-model="dataForm.supplierStatus" placeholder="状态：0禁止，1启用"></el-input>
    </el-form-item>
    <el-form-item label="最后修改时间" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder="最后修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          supplierId: 0,
          supplierCode: '',
          supplierName: '',
          supplierType: '',
          linkMan: '',
          phoneNumber: '',
          bankName: '',
          bankAccount: '',
          address: '',
          supplierStatus: '',
          modifiedTime: '',
          deleted: ''
        },
        dataRule: {
          supplierCode: [
            { required: true, message: '供应商编码不能为空', trigger: 'blur' }
          ],
          supplierName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          supplierType: [
            { required: true, message: '供应商类型：1.自营，2.平台不能为空', trigger: 'blur' }
          ],
          linkMan: [
            { required: true, message: '供应商联系人不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          bankName: [
            { required: true, message: '供应商开户银行名称不能为空', trigger: 'blur' }
          ],
          bankAccount: [
            { required: true, message: '银行账号不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '供应商地址不能为空', trigger: 'blur' }
          ],
          supplierStatus: [
            { required: true, message: '状态：0禁止，1启用不能为空', trigger: 'blur' }
          ],
          modifiedTime: [
            { required: true, message: '最后修改时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.supplierId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.supplierId) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/supplierinfo/info/${this.dataForm.supplierId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.supplierCode = data.supplierInfo.supplierCode
                this.dataForm.supplierName = data.supplierInfo.supplierName
                this.dataForm.supplierType = data.supplierInfo.supplierType
                this.dataForm.linkMan = data.supplierInfo.linkMan
                this.dataForm.phoneNumber = data.supplierInfo.phoneNumber
                this.dataForm.bankName = data.supplierInfo.bankName
                this.dataForm.bankAccount = data.supplierInfo.bankAccount
                this.dataForm.address = data.supplierInfo.address
                this.dataForm.supplierStatus = data.supplierInfo.supplierStatus
                this.dataForm.modifiedTime = data.supplierInfo.modifiedTime
                this.dataForm.deleted = data.supplierInfo.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/supplierinfo/${!this.dataForm.supplierId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'supplierId': this.dataForm.supplierId || undefined,
                'supplierCode': this.dataForm.supplierCode,
                'supplierName': this.dataForm.supplierName,
                'supplierType': this.dataForm.supplierType,
                'linkMan': this.dataForm.linkMan,
                'phoneNumber': this.dataForm.phoneNumber,
                'bankName': this.dataForm.bankName,
                'bankAccount': this.dataForm.bankAccount,
                'address': this.dataForm.address,
                'supplierStatus': this.dataForm.supplierStatus,
                'modifiedTime': this.dataForm.modifiedTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
